package com.oahcfly.chgame.test;

import java.util.HashMap;
import java.util.HashSet;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.utils.Align;
import com.oahcfly.chgame.core.Chao;
import com.oahcfly.chgame.core.freefont.CHFreeBitmapFont;
import com.oahcfly.chgame.core.listener.CHClickListener;
import com.oahcfly.chgame.core.listener.CHClickListener.CLICKTYPE;
import com.oahcfly.chgame.core.mvc.CHActor;
import com.oahcfly.chgame.core.mvc.CHGame;
import com.oahcfly.chgame.core.mvc.CHScreen;
import com.oahcfly.chgame.core.script.CHScript;
import com.oahcfly.chgame.egret.EXMLReader;
import com.oahcfly.chgame.egret.ui.BaseSkinUI;
import com.oahcfly.chgame.egret.ui.ERes;

public class NormalScreen extends CHScreen {

	NormalModel normalModel;

	@Override
	public void initScreen() {
		// TODO Auto-generated method stub
		normalModel = new NormalModel();
		setModel(normalModel);
		
		EXMLReader exmlReader = new EXMLReader();
		exmlReader.parseExml("StartUISkin.exml");

		Image image = CHGame.getInstance().getImage("badlogic.jpg");
		image.setSize(480, 800);
		image.addListener(new CHClickListener(CLICKTYPE.NORMAL) {

			@Override
			public void doClicked(InputEvent event, float x, float y) {

				// CHGame.getInstance().setScreen(new NormalScreen2());
				// new CHScreenTransitionSlide(2f,
				// CHScreenTransitionSlide.TO_RIGHT, false));
				// new CHScreenTransitionFade(1.5f));

				// CHAutoParticle chAutoParticle =
				// CHAutoParticle.obtain("star_1.png");
				// chAutoParticle.playCircleParticle(getStage(), x, y);

				// chAutoParticle.playFireWorks(getStage(), new Rectangle(0, 0,
				// 400, 800), 3, null);
				normalModel.addScore();
				System.out.println("addScroe");

				for (int i = 0; i < 1; i++) {
					Image image = Chao.plistCenter.getImageFromPList("Cross0.plist", "ProgressBar1.png");

					image.setY(100 * i);
					addActor(image);

				}

				TextureRegion region = Chao.plistCenter.getTextureRegion("Cross0.plist", "ProgressBar1.png");
				CHActor chActor = new CHActor();

				chActor.setBgTextureRegion(region);

				chActor.setOrigin(Align.center);
				addActor(chActor);

			}

		});

		testPlist();

		addActor(image);

		// CHCountDownAction chCountDownAction = new CHCountDownAction();
		// Array<Image> imgArray = new Array<Image>();
		// imgArray.add(Chao.game.getImage("badlogic.jpg"));
		// imgArray.add(Chao.game.getImage("sprite.png"));
		// chCountDownAction.setDuration(2);
		// chCountDownAction.setImgArray(imgArray );
		// image.addAction(chCountDownAction);

		// CHPListCenter.getInstance().getTextureRegion("sprite.plist",
		// "chuizi4.png");
		// chActor.addAction(CHActions.createAutoScaleToCenter(2f, 1, 3));

		// testFont();

		CHScript script = new CHScript(this);
		script.addSteps("step1", "step2", "step3");
//		script.run(new Runnable() {
//			
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				//System.out.print("step执行完毕");
//			}
//		});
//		
		
		BaseSkinUI baseSkinUI = new BaseSkinUI(exmlReader.getUILayerProperty()) {
			
			@Override
			public void createChildComplete() {
				// TODO Auto-generated method stub
				System.out.println("baseSkinUI create ok...");
			}

			@Override
			public void preloadRes() {
				// TODO Auto-generated method stub
				ERes.loadRes("loading_bg_jpg", "exml/loading_bg.jpg");
				ERes.loadRes("menu_adventure2_png", "exml/menu_adventure2.png");
				ERes.loadRes("menu_adventure_png", "exml/menu_adventure.png");
				ERes.loadRes("snum_fnt", "exml/snum.fnt");
			}
		};
		addActor(baseSkinUI);
	}

	public void step1() {
		System.out.println(System.currentTimeMillis()+"  step1");
	}

	public void step2() {
		System.out.println(System.currentTimeMillis()+"  step2");
	}

	public void step3() {
		System.out.println(System.currentTimeMillis()+"  step3");
	}

	private void testPlist() {
		// 测试plist
		long startime = System.currentTimeMillis();

		HashSet<String> pngSet = Chao.plistCenter.getPlistPngNameSet("Cross0.plist");
		for (String imgName : pngSet) {
			Image img = Chao.plistCenter.getImageFromPList("Cross0.plist", imgName);
			addActor(img);
		}

		System.out.println("consume time :" + (System.currentTimeMillis() - startime));
	}

	private void testFont() {
		// TODO Auto-generated method stub
		// [静态文本:一次性创建所需字符，内部纹理大小只够装下初始化的字符，不支持纹理动态增加，内部不保留PixmapPacker,所需内存最小]
		CHFreeBitmapFont font0 = Chao.game.getFreeFont();
		font0.createText("开始分解图片");
		Label label0 = new Label("开始分解", new LabelStyle(font0, Color.WHITE));
		label0.setY(800 - label0.getHeight());
		addActor(label0);
		label0.addListener(new CHClickListener() {

			@Override
			public void doClicked(InputEvent event, float x, float y) {
				// TODO Auto-generated method stub

			}

		});
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		super.draw();
	}

	@Override
	public void endScreen() {
		// TODO Auto-generated method stub

	}

	@Override
	public void clickBackKey() {
		// TODO Auto-generated method stub
		CHGame.getInstance().setScreen(new NormalScreen2());
	}

	@Override
	public void handleUIEvent(HashMap<String, String> data) {
		System.out.println("data:" + data);
	}

}
